
from PySide6.QtCore import QFile
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QMessageBox

from src.main.server.controller.score_controller import ScoreController
from src.main.server.controller.server_feedback_controller import ServerFeedbackController

class EditFeedbackWindow:

    def __init__(self, main_window, data=None):
        self.status = None
        self.original_data = None
        self.data = None
        if data:
            self.data = data
        self._load_ui()
        self.main_window = main_window

    def _load_ui(self):
        path = "../../../qt_ui/edit_feedback.ui"
        file = QFile(path)
        try:
            loader = QUiLoader()
            self.window = loader.load(file)
            self.window.pushButton_2.clicked.connect(self.close_window)
            self.window.buttonGroup.buttonClicked.connect(self.handle_status_selection)

        finally:
            file.close()

        if self.window:
            self.window.show()  # 显示独立窗口
        else:
            print("加载 UI 失败，请检查文件路径或内容")

        if self.data:
            """有数据则回显数据"""
            self.get_info()

    def handle_status_selection(self):
        """按钮组的动态获取"""
        selected_button = self.window.buttonGroup.checkedButton()
        if selected_button:
            self.status = selected_button.text()
        else:
            self.status = None

    def get_info(self):
        self.window.lineEdit.setText(self.data[0][0])           #项目名称
        self.window.lineEdit_2.setText(self.data[0][1])         #申请人

        status = str(self.data[0][2])
        if status == "1":
            status = "进行中"
        elif status == "2":
            status = "已结束"
        else:
            status = "未开始"

        for button in self.window.buttonGroup.buttons():
            if button.text() == status:
                button.setChecked(True)
                break

        self.window.plainTextEdit.setPlainText(self.data[0][3])

        self.window.pushButton.clicked.connect(self.submit)

    def submit(self):

        server_name = self.window.lineEdit.text()
        vol_name = self.window.lineEdit_2.text()
        if self.status == "进行中":
            status = "1"
        elif self.status == "已结束":
            status = "2"
        else:
            status = "0"

        feedback = self.window.plainTextEdit.toPlainText()

        """当数据状态为结束时，应该增添完成项目获得的积分"""
        if status == "2":
            #1.增加对应员工积分
            status_code = ScoreController().insert((vol_name, server_name))
            if status_code is None:
                #该项目积分已添加过
                QMessageBox.information(self.window, "提示", "请勿重复加分哦")
                return
        #2.更新数据
        ServerFeedbackController().update((status, feedback, server_name, vol_name))
        print("推送成功")
        self.main_window.new_data()
        self.close_window()

    def close_window(self):
        """关闭当前窗口"""
        self.window.close()

def edit_feedback_window(self, data = None):
    if data:
        self.edit_window = EditFeedbackWindow(self, data)
    else:
        self.edit_window = EditFeedbackWindow(self)